﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Frm_Login : UIForm
    {
        public Frm_Login()
        {
            InitializeComponent();
        }
        //创建数据库连接
        db_mmsEntities conn = new db_mmsEntities();
        List<string> nb_List = new List<string>();
        List<string> pw_List = new List<string>();
        
        private void Frm_Login_Load(object sender, EventArgs e)
        {
            txt_Number.TextChanged += Txt_Number_TextChanged;
            txt_pw.ButtonClick += Login_Click;
            lbe_sign.Click += Lbe_sign_Click;
            InitData();

        }

        private void Lbe_sign_Click(object sender, EventArgs e)
        {
            Frm_Sign frm = new Frm_Sign();
            frm.ShowDialog();
        }

        /// <summary>
        /// 账号框内容改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Txt_Number_TextChanged(object sender, EventArgs e)
        {
            string str1 = txt_Number.Text;
            txt_pw.Clear();
            if (str1.Length == 6 || str1.Length == 8)
            {
                int index = nb_List.IndexOf(str1);
                if (index != -1)
                {
                    txt_pw.Text = pw_List[index];
                }
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_Click(object sender, EventArgs e)
        {
            string inputNb=txt_Number.Text;
            string inputPw=txt_pw.Text;
            if (inputNb.Length == 6)
            {
                //管理员登录
                Login(inputNb, inputPw, 1);
                //MessageBox.Show("管理员登录");
            }
            else if(inputNb.Length==8)
            {
                //员工登录
                Login(inputNb, inputPw, 0);

                //MessageBox.Show("员工登录");
            }
            else
            {
                MessageBox.Show("账号输入有误！");
            }

            
        }



        private void InitData()
        {
            //读取Excel文件内账号密码
            
            MyExcel excel = new MyExcel(@".\UandP.xlsx", "UandP");
            nb_List = excel.ReadWrite_ColumnValue(1);
            pw_List = excel.ReadWrite_ColumnValue(2);
            txt_Number.DataSource = nb_List;
        }
        
        /// <summary>
        /// 更新Excel
        /// </summary>
        private void Updata(string number,string password)
        {
            if (check_Record.Checked)
            {
                if (nb_List.IndexOf(number) == -1)
                {
                    //Console.WriteLine("不存在");
                    MyExcel excel = new MyExcel(@".\UandP.xlsx", "UandP");

                    excel.ColumnAdd(1, number);
                    excel.ColumnAdd(2, password);
                }
            }
        }

        /// <summary>
        /// 判断账号密码是否匹配
        /// </summary>
        /// <param name="number"></param>
        /// <param name="password"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private void Login(string number,string password,int type)
        {
            if (type == 0)
            {
                //var temp = (from i in conn.tb_staff
                //            where i.staffID == number && i.password == password
                //            select i
                //            ).ToList();
                var temp = conn.tb_staff.FirstOrDefault(r => r.staffID == number && r.password == password);
                //Console.WriteLine(temp.Count());
                if (temp==null)
                {
                    MessageBox.Show("登录失败，请检查你的工号或密码！");
                }
                else
                {
                    if (temp.isUse == 0)
                    {
                        MessageBox.Show("账号未激活，请联系管理员激活");
                        return;
                    }
                    Updata(number, password);

                    Frm_Staff frm_Staff = new Frm_Staff { staffID = temp.staffID,staffName=temp.staffName, staffStore=temp.storeName };
                    this.Hide();
                    frm_Staff.Show();
                }
            }
            else if (type == 1)
            {
                var temp = conn.tb_root.FirstOrDefault(r=>r.rootID==number&&r.password==password);
                //Console.WriteLine(temp.Count());
                if (temp==null)
                {
                    MessageBox.Show("登录失败，请检查你的账号或密码！");
                }
                else
                {
                    Updata(number,password);
                    Frm_Root frm_Root = new Frm_Root { rootID = temp.rootID, storename=temp.storeName };
                    this.Hide();
                    frm_Root.Show();
                    
                }
            }
            
        }
    }
}
